package com.yunxin.core.application.service.impl;

import com.yunxin.common.exception.BusinessException;
import com.yunxin.common.result.PageResult;
import com.yunxin.common.result.ResultCode;
import com.yunxin.core.application.assembler.MerchantAssembler;
import com.yunxin.core.application.command.merchant.MerchantPageCommand;
import com.yunxin.core.application.dto.merchant.*;
import com.yunxin.core.application.service.MerchantQueryAdminService;
import com.yunxin.core.infra.persistence.entity.merchant.MerchantContactPersonDO;
import com.yunxin.core.infra.persistence.entity.merchant.MerchantInfoDO;
import com.yunxin.core.infra.persistence.entity.merchant.MerchantInvoiceInfoDO;
import com.yunxin.core.infra.persistence.entity.merchant.MerchantUserAccountDO;
import com.yunxin.core.infra.persistence.entity.merchant.query.condition.MerchantPageCondition;
import com.yunxin.core.infra.persistence.entity.merchant.query.result.MerchantListResult;
import com.yunxin.core.infra.persistence.entity.merchant.query.result.MerchantPageResult;
import com.yunxin.core.infra.persistence.mapper.merchant.MerchantContactPersonExtendMapper;
import com.yunxin.core.infra.persistence.mapper.merchant.MerchantInfoExtendMapper;
import com.yunxin.core.infra.persistence.mapper.merchant.MerchantInvoiceInfoExtendMapper;
import com.yunxin.core.infra.persistence.mapper.merchant.MerchantUserAccountExtendMapper;
import com.yunxin.core.utils.infra.utils.PageQueryUtils;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author zero°
 * @since 2023-07-12
 */
@Service
@RequiredArgsConstructor
public class MerchantQueryAdminServiceImpl implements MerchantQueryAdminService {

    private final MerchantInfoExtendMapper merchantInfoExtendMapper;

    private final MerchantContactPersonExtendMapper merchantContactPersonExtendMapper;

    private final MerchantInvoiceInfoExtendMapper merchantInvoiceInfoExtendMapper;

    private final MerchantUserAccountExtendMapper merchantUserAccountExtendMapper;

    private final MerchantAssembler merchantAssembler;

    @Override
    public List<MerchantListDTO> selectList(String merchantName) {
        List<MerchantListResult> merchantListResults = merchantInfoExtendMapper.selectList(merchantName);
        return  merchantListResults.stream().map(merchantAssembler::toMerchantListDTO).toList();
    }

    @Override
    public List<MerchantListDTO> listMerchantByIds(Set<Long> merchantIds) {
        List<MerchantListResult> results = merchantInfoExtendMapper.listMerchantByIds(merchantIds);
        return results.stream().map(merchantAssembler::toMerchantListDTO).toList();
    }

    @Override
    public MerchantDetailDTO getMerchantDetail(Long id) {
        MerchantInfoDO merchantInfoDO = merchantInfoExtendMapper.selectByPrimaryKey(id);
        if (Objects.isNull(merchantInfoDO)) {
            throw new BusinessException(ResultCode.DATA_NO_EXIST_ERROR);
        }
        MerchantDetailDTO merchantDetailDTO = merchantAssembler.toMerchantDetailDTO(merchantInfoDO);
        MerchantContactPersonDO merchantContactPersonDO = merchantContactPersonExtendMapper.selectMerchantContactPersonByMerchantId(merchantInfoDO.getId());
        MerchantContactPersonDTO merchantContactPersonDTO = merchantAssembler.toMerchantContactPersonDTO(merchantContactPersonDO);
        merchantDetailDTO.setMerchantContactPerson(merchantContactPersonDTO);
        MerchantInvoiceInfoDO merchantInvoiceInfoDO = merchantInvoiceInfoExtendMapper.selectMerchantInvoiceInfoByMerchantId(merchantInfoDO.getId());
        MerchantInvoiceDTO merchantInvoiceDTO = merchantAssembler.toMerchantInvoiceDTO(merchantInvoiceInfoDO);
        merchantDetailDTO.setMerchantInvoice(merchantInvoiceDTO);
        return merchantDetailDTO;
    }

    @Override
    public List<MerchantListDTO> listMerchant(Integer merchantType) {
        List<MerchantListResult> results = merchantInfoExtendMapper.listMerchant(merchantType);
        return results.stream().map(merchantAssembler::toMerchantListDTO).toList();
    }

    @Override
    public PageResult<MerchantPageDTO> merchantPageQuery(MerchantPageCommand command) {
        MerchantPageCondition condition = merchantAssembler.fromMerchantPageCommand(command);
        long total = merchantInfoExtendMapper.countMerchantPageQuery(condition);
        if (total == 0) {
            return PageResult.success(Collections.emptyList(), total);
        }
        condition.setOffset(PageQueryUtils.X.getOffset(command.getPageNum(), command.getPageSize()));
        List<MerchantPageResult> results = merchantInfoExtendMapper.merchantPageQuery(condition);
        List<MerchantPageDTO> dtoList = results.stream().map(merchantAssembler::toMerchantPageDTO).toList();
        return PageResult.success(dtoList, total);
    }

    @Override
    public List<MerchantUserAccountDTO> listMerchantUserAccountBySysUserIds(@NotEmpty(message = "系统用户id集合不能为空") List<Long> sysUserIds) {
        List<MerchantUserAccountDO> list = merchantUserAccountExtendMapper.listMerchantUserAccountBySysUserIds(sysUserIds);
        return list.stream().map(merchantAssembler::toMerchantUserAccountDTO).toList();
    }

    @Override
    public List<MerchantUserAccountInfoDTO> listMerchantUserAccountByMerchantId(@NotNull(message = "商户id不能为空") Long merchantId) {



        List<MerchantUserAccountDO> list = merchantUserAccountExtendMapper.listMerchantUserAccountByMerchantId(merchantId);
        return list.stream().map(merchantAssembler::toMerchantUserAccountInfoDTO).toList();
    }

    @Override
    public List<MerchantPageDTO> merchantPageQueryList(MerchantPageCommand command) {
        return null;
    }

}
